package com.supor.aiot.home.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.supor.aiot.home.R;

import androidx.annotation.DrawableRes;


public class ConfigTopView extends RelativeLayout {
    TextView tvStep1;
    TextView tvStep2;
    TextView tvStep3;
    TextView tvConfig1;
    TextView tvConfig2;
    TextView tvConfig3;
    View lineRight1;
    View lineLeft2;
    View lineRight2;
    View lineLeft3;
    View viewBg;

    public ConfigTopView(Context context) {
        this(context, null);
    }

    public ConfigTopView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ConfigTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ConfigTopView);
        CharSequence config1_text = typedArray.getText(R.styleable.ConfigTopView_config1_text);
        CharSequence config2_text = typedArray.getText(R.styleable.ConfigTopView_config2_text);
        int config1_drawable =
                typedArray.getResourceId(R.styleable.ConfigTopView_config1_drawable, -1);
        int config2_drawable =
                typedArray.getResourceId(R.styleable.ConfigTopView_config2_drawable, -1);
        int config3_drawable =
                typedArray.getResourceId(R.styleable.ConfigTopView_config3_drawable, -1);
        int config1_text_color = typedArray.getColor(R.styleable.ConfigTopView_config1_text_color
                , context.getResources().getColor(R.color.content_default));
        int config2_text_color = typedArray.getColor(R.styleable.ConfigTopView_config2_text_color
                , context.getResources().getColor(R.color.content_default));
        int config3_text_color = typedArray.getColor(R.styleable.ConfigTopView_config3_text_color
                , context.getResources().getColor(R.color.content_default));
        int line_right1_color =
                typedArray.getResourceId(R.styleable.ConfigTopView_line_right1_color,
                        R.color.colorPrimary);
        int line_right2_color =
                typedArray.getResourceId(R.styleable.ConfigTopView_line_right2_color,
                        R.color.colorPrimary);
        int line_left2_color =
                typedArray.getResourceId(R.styleable.ConfigTopView_line_left2_color,
                        R.color.colorPrimary);
        int line_left3_color =
                typedArray.getResourceId(R.styleable.ConfigTopView_line_left3_color,
                        R.color.colorPrimary);
        int padding_top =
                (int) typedArray.getDimension(R.styleable.ConfigTopView_config_padding_top,
                        context.getResources().getDimension(R.dimen.dp_0));
        int bg_resource = typedArray.getResourceId(R.styleable.ConfigTopView_config_bg_resource,
                -1);

        initView();
        setPadding(getPaddingLeft(), padding_top, getPaddingRight(), getPaddingBottom());

        if (!TextUtils.isEmpty(config1_text)) {
            setConfig1Text((String) config1_text);
        }

        if (!TextUtils.isEmpty(config2_text)) {
            setConfig2Text((String) config2_text);
        }

        setConfig1TextColor(config1_text_color);
        setConfig2TextColor(config2_text_color);
        setConfig3TextColor(config3_text_color);

        setLineRight1Color(line_right1_color);
        setLineRight2Color(line_right2_color);
        setLineLeft2Color(line_left2_color);
        setLineLeft3Color(line_left3_color);

        if (config1_drawable != -1) {
            setConfig1Drawable(config1_drawable);
        }
        if (config2_drawable != -1) {
            setConfig2Drawable(config2_drawable);
        }
        if (config3_drawable != -1) {
            setConfig3Drawable(config3_drawable);
        }
        if (bg_resource != -1) {
            viewBg.setBackgroundResource(bg_resource);
        }
    }

    private void initView() {
        View.inflate(getContext(), R.layout.layout_config_status, this);
        tvStep1 = findViewById(R.id.tv_top_step1);
        tvStep2 = findViewById(R.id.tv_top_step2);
        tvStep3 = findViewById(R.id.tv_top_step3);
        tvConfig1 = findViewById(R.id.tv_top_config1);
        tvConfig2 = findViewById(R.id.tv_top_config2);
        tvConfig3 = findViewById(R.id.tv_top_config3);
        lineRight1 = findViewById(R.id.line_top_right1);
        lineRight2 = findViewById(R.id.line_top_right2);
        lineLeft2 = findViewById(R.id.line_top_left2);
        lineLeft3 = findViewById(R.id.line_top_left3);
        viewBg = findViewById(R.id.view_bg);
    }

    public void setConfig1Text(String text) {
        tvConfig1.setText(text);
    }

    public void setConfig2Text(String text) {
        tvConfig2.setText(text);
    }

    public void setConfig1Drawable(@DrawableRes int resId) {
        // ivConfig1.setImageResource(resId);
        tvStep1.setBackgroundResource(resId);
    }

    public void setConfig2Drawable(@DrawableRes int resId) {
        // ivConfig2.setImageResource(resId);
        tvStep2.setBackgroundResource(resId);
    }

    public void setConfig3Drawable(@DrawableRes int resId) {
        // ivConfig3.setImageResource(resId);
        tvStep3.setBackgroundResource(resId);
    }

    public void setConfig1TextColor(int textColor) {
        tvConfig1.setTextColor(textColor);
    }

    public void setConfig2TextColor(int textColor) {
        tvConfig2.setTextColor(textColor);
    }

    public void setConfig3TextColor(int textColor) {
        tvConfig3.setTextColor(textColor);
    }

    public void setLineRight1Color(int textColor) {
        lineRight1.setBackgroundResource(textColor);
    }

    public void setLineRight2Color(int textColor) {
        lineRight2.setBackgroundResource(textColor);
    }

    public void setLineLeft2Color(int textColor) {
        lineLeft2.setBackgroundResource(textColor);
    }

    public void setLineLeft3Color(int textColor) {
        lineLeft3.setBackgroundResource(textColor);
    }
}
